package org.example.env_boot.controller;

import org.example.env_boot.dto.ApiResponse;
import org.example.env_boot.service.AuthService;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.Map;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@CrossOrigin(origins = "*")
public class AuthController {
    
    private final AuthService authService;
    
    public AuthController(AuthService authService) {
        this.authService = authService;
    }
    
    /**
     * 测试接口 - 不需要任何认证
     */
    @GetMapping("/test")
    public ApiResponse<String> test() {
        return ApiResponse.success("认证控制器正常工作");
    }
    
    /**
     * 管理员登录
     */
    @PostMapping("/admin/login")
    public ApiResponse<Map<String, Object>> adminLogin(@RequestBody AdminLoginRequest request) {
        return authService.adminLogin(request.getAdminCode(), request.getPassword());
    }
    
    /**
     * 网格员登录
     */
    @PostMapping("/grid/login")
    public ApiResponse<Map<String, Object>> gridMemberLogin(@RequestBody GridMemberLoginRequest request) {
        return authService.gridMemberLogin(request.getGmCode(), request.getPassword());
    }
    
    /**
     * 公众监督员登录
     */
    @PostMapping("/supervisor/login")
    public ApiResponse<Map<String, Object>> supervisorLogin(@RequestBody SupervisorLoginRequest request) {
        return authService.supervisorLogin(request.getSupervisorCode(), request.getPassword());
    }
    
    /**
     * 公众监督员注册
     */
    @PostMapping("/supervisor/register")
    public ApiResponse<?> supervisorRegister(@RequestBody SupervisorRegisterRequest request) {
        return authService.supervisorRegister(
            request.getSupervisorCode(), 
            request.getPassword(), 
            request.getSupervisorName(), 
            request.getSupervisorPhone(), 
            request.getProvinceId(),
            request.getCityId()
        );
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/userinfo")
    public ApiResponse<Map<String, Object>> getUserInfo(@RequestHeader("Authorization") String authorization) {
        if (authorization != null && authorization.startsWith("Bearer ")) {
            String token = authorization.substring(7);
            return authService.getUserInfo(token);
        }
        return ApiResponse.unauthorized("未提供有效的认证信息");
    }
    
    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public ApiResponse<?> logout() {
        // JWT是无状态的，客户端删除token即可
        return ApiResponse.success("退出登录成功");
    }
    
    // 内部请求类
    public static class AdminLoginRequest {
        @NotBlank(message = "管理员编码不能为空")
        private String adminCode;
        
        @NotBlank(message = "密码不能为空")
        private String password;
        
        public String getAdminCode() { return adminCode; }
        public void setAdminCode(String adminCode) { this.adminCode = adminCode; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }
    
    public static class GridMemberLoginRequest {
        @NotBlank(message = "网格员编码不能为空")
        private String gmCode;
        
        @NotBlank(message = "密码不能为空")
        private String password;
        
        public String getGmCode() { return gmCode; }
        public void setGmCode(String gmCode) { this.gmCode = gmCode; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }
    
    public static class SupervisorLoginRequest {
        @NotBlank(message = "监督员编号不能为空")
        private String supervisorCode;
        
        @NotBlank(message = "密码不能为空")
        private String password;
        
        public String getSupervisorCode() { return supervisorCode; }
        public void setSupervisorCode(String supervisorCode) { this.supervisorCode = supervisorCode; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }
    
    public static class SupervisorRegisterRequest {
        @NotBlank(message = "监督员编号不能为空")
        private String supervisorCode;
        
        @NotBlank(message = "密码不能为空")
        private String password;
        
        @NotBlank(message = "监督员姓名不能为空")
        private String supervisorName;
        
        @NotBlank(message = "手机号不能为空")
        private String supervisorPhone;
        
        @NotNull(message = "省份ID不能为空")
        private Integer provinceId;
        
        @NotNull(message = "城市ID不能为空")
        private Integer cityId;
        
        public String getSupervisorCode() { return supervisorCode; }
        public void setSupervisorCode(String supervisorCode) { this.supervisorCode = supervisorCode; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public String getSupervisorName() { return supervisorName; }
        public void setSupervisorName(String supervisorName) { this.supervisorName = supervisorName; }
        public String getSupervisorPhone() { return supervisorPhone; }
        public void setSupervisorPhone(String supervisorPhone) { this.supervisorPhone = supervisorPhone; }
        public Integer getProvinceId() { return provinceId; }
        public void setProvinceId(Integer provinceId) { this.provinceId = provinceId; }
        public Integer getCityId() { return cityId; }
        public void setCityId(Integer cityId) { this.cityId = cityId; }
    }
} 